package com.qfedu.service.impl;

import com.qfedu.dao.BillDao;
import com.qfedu.dao.CargoreceiptdetailDao;
import com.qfedu.dao.GoodsBillDao;
import com.qfedu.dao.GoodsbilleventDao;
import com.qfedu.entity.*;
import com.qfedu.service.GoodsBillService;
import com.qfedu.util.Constants;
import com.qfedu.util.RandomStringUtil;
import com.qfedu.util.Result;
import com.qfedu.util.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * projectName: logistics
 *
 * @author: CGY
 * time: 2020-10-7 20:51
 * description:
 */
@Service
public class GoodsBillServiceImpl implements GoodsBillService {
    @Autowired
    private GoodsBillDao goodsBillDao;
    @Autowired
    private BillDao billDao;
    @Autowired
    private GoodsbilleventDao goodsbilleventDao;
    @Autowired
    private CargoreceiptdetailDao cargoreceiptdetailDao;

    private String prefix = "HY";
    private String prefix1 = "HZ";
    private String getCode(String prefix){
        String code = null;
        while(true){
            String s = RandomStringUtil.RandString(6);
            code = prefix +  s;
            Goodsbill goods = goodsBillDao.findByGoodsBillCode(code);
            if(goods == null){
                break;
            }
        }
        return code;
    }
    @Transactional(rollbackFor = Exception.class)
    @Override
    public Result add(Goodsbill goodsbill) {
        //三件事
        //第一 生成货运单编号 HY开头 然后保存货运单
        //第二 在票据分发里添加票据 类型为货运单 状态为未分发
        //第三 在货运单事件表里 添加记录 事件名为待发货
        String code = getCode(prefix);
        goodsbill.setGoodsBillCode(code);
        goodsbill.setIfAudit(false);
        goodsbill.setValidity(false);
        Goodsbill good = goodsBillDao.save(goodsbill);

        Billinfo billinfo = new Billinfo();
        billinfo.setAcceptStation(good.getAcceptStation());
        billinfo.setBillCode(good.getGoodsBillCode());
        billinfo.setBillType(Constants.BILL_TYPE_HY);
        billinfo.setWriteDate(good.getWriteDate());
        billinfo.setBillState(Constants.BILL_UN_RELEASE);
        Billinfo bill = billDao.save(billinfo);

        Goodsbillevent goodsbillevent = new Goodsbillevent();
        goodsbillevent.setGoodsBillId(good.getGoodsBillCode());
        goodsbillevent.setEventName(Constants.EVENT_UN_SEND);
        goodsbillevent.setOccurTime(good.getWriteDate());
        goodsbillevent.setRemark("单据已填");
        Goodsbillevent save = goodsbilleventDao.save(goodsbillevent);

        return Result.success(good);

    }

    @Override
    public Result addGoods(Cargoreceiptdetail cargoreceiptdetail) {
        //要干三件事
        //添加货运回执单
        // 第一 添加货物信息
        //修改货运单的审核 和有效状态




        //2
        Goodsbill good = goodsBillDao.findByGoodsBillCode(cargoreceiptdetail.getGoodsBillDetailId());
        good.setIfAudit(true);
        good.setValidity(true);
        Goodsbill good1 = goodsBillDao.save(good);
        //3
        String code = getCode(prefix1);
        Billinfo billinfo = new Billinfo();
        billinfo.setBillCode(code);
        //设置货运回执单状态为待送货
        billinfo.setBillState(Constants.HZ_BILL_WRITED);
        billinfo.setBillType(Constants.BILL_TYPE_HZ);
        billinfo.setAcceptStation(good1.getAcceptStation());
        billinfo.setWriteDate(new Date());
        Billinfo save = billDao.save(billinfo);
        //1
        cargoreceiptdetail.setGoodsRevertBillId(billinfo.getBillCode());
        cargoreceiptdetailDao.save(cargoreceiptdetail);
        return Result.success("成功");

    }

    @Override
    public Result selectByEvent(String eventName) {
        List<Goodsbillevent> list = new ArrayList<>();
        if("".equals(eventName)){
            list = goodsbilleventDao.findAll();
        }else{
            list = goodsbilleventDao.findAllByEventName(eventName);
        }

        return ResultUtil.getObject(list,"查询失败");
    }

    @Override
    public Result selectByCode(String goodsBillCode) {

        Goodsbill good = goodsBillDao.findByGoodsBillCode(goodsBillCode);
        return ResultUtil.getObject(good,"查询失败");
    }

    @Override
    public Result update(Goodsbill goodsbill) {
        Goodsbill good = goodsBillDao.save(goodsbill);
        return ResultUtil.getObject(good,"修改失败");
    }
}
